package com.driving.drivingexam.controller;

import com.driving.drivingexam.common.ServerResponse;
import com.driving.drivingexam.common.entity.Titles;
import com.driving.drivingexam.common.entity.User;
import com.driving.drivingexam.mapper.UserRepository;
import com.driving.drivingexam.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    UserRepository userRepository;

    @GetMapping("/list")
    public String queryAll(ModelMap modelMap){
        List<User> users = userService.queryAllUser();
        modelMap.put("content","user-list");
        modelMap.put("users",users);
        return "index";
    }
    @ResponseBody
    @PostMapping("/add")
    public ServerResponse addUser(@RequestBody Map<String,Object> map){

        String username = (String) map.get("username");
        String password = (String) map.get("password");
        String phone = (String) map.get("phone");
        String idCard = (String) map.get("idCard");
        String driverSchool = (String) map.get("driverSchool");
        String studySubject = (String) map.get("studySubject");
        Double fee = Double.parseDouble( (String) map.get("fee"));
        String vipTime = (String) map.get("vipTime");
        Date createTime =  new Date();
        Date updateTime = new Date();

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setIdCard(idCard);
        user.setDriverSchool(driverSchool);
        user.setStudySubject(studySubject);
        user.setFee(fee);
        user.setPhone(vipTime);
        user.setCreateTime(createTime);
        user.setUpdateTime(updateTime);
        user.setIsDelete(1);
        System.out.println(user);
        userService.addUser(user);

        ServerResponse<Object> bySuccess = ServerResponse.createBySuccessMessage(username);
        return bySuccess;
    }

    @ResponseBody
    @GetMapping("/show")
    public ServerResponse show(ModelMap modelMap,String id ){

        Integer uid = Integer.parseInt(id);
        System.out.println(uid);
        User uu = userService.showInfo(uid);
        User one = userRepository.getOne(uid);
        String substring = uu.toString().substring(uu.toString().indexOf("{")+1, uu.toString().lastIndexOf("}"));
        String ss = substring.replaceAll(",","\n");
        int i=1;
        List<Titles> titles = one.getTitles();
        for (Titles t:
                titles) {
            ss=ss+"\n"+"套题"+i+":"+t.getTitlesName();
            i=i+1;
        }
        ServerResponse<Object> bySuccess = ServerResponse.createBySuccessMessage(ss);
        return bySuccess;
    }

    @PutMapping("/")
    public void update(@RequestBody User user){
        userService.updateUserDel(user);
    }

    @GetMapping("/loginUI")
    public String loginUI(){
        return "page/login";
    }

    @GetMapping("/login")
    @ResponseBody
    public ServerResponse login(ModelMap modelMap, String username,String password){

        System.out.println(username);
        User user = userService.selectByName(username);
        if (user==null){
            ServerResponse<Object> byErrorCode = ServerResponse.createByErrorCodeMessage(400, "用户不存在！");
            return byErrorCode;
        }else {

            if (user.getPassword().equals(password)){
                ServerResponse<Object> bySuccess = ServerResponse.createBySuccess();
                return bySuccess;
            }else {

                ServerResponse<Object> byErrorCodeMessage = ServerResponse.createByErrorCodeMessage(400,"密码不正确，请重新输入！");
                return byErrorCodeMessage;

            }
        }

    }
}
